import {
    showAllauthor,
    showWriterByStateOrWriterName,
    uploadWriterImage,
    addWriterInfo,
    updateWriterState,
    updateWriterInfo,
    deleteWriterInfo
} from "../../api/author";
import {message} from "antd";

export default{
    authordata:[
        // {
        //     id:1,
        //     App:'小黑早教',
        //     picture:'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fup.enterdesk.com%2Fedpic%2Ffe%2F11%2F97%2Ffe119792285b42e687ebf4367f77c8de.jpg&refer=http%3A%2F%2Fup.enterdesk.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1667467708&t=f250a0a76a390e652a8e7d302b64f987',
        //     name:'史铁生',
        //     status:'正常',
        //     createtime:'2022-06-01 14:45:00',
        //     des:'作家、散文家'
        // },
        // {
        //     id:2,
        //     App:'小黑早教',
        //     picture:'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fup.enterdesk.com%2Fedpic%2Ffe%2F11%2F97%2Ffe119792285b42e687ebf4367f77c8de.jpg&refer=http%3A%2F%2Fup.enterdesk.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1667467708&t=f250a0a76a390e652a8e7d302b64f987',
        //     name:'余华',
        //     status:'冻结',
        //     createtime:'2022-06-26 10:21:49',
        //     des:"当代作家，代表作《活着》"
        // },
        // {
        //     id:3,
        //     App:'小黑早教',
        //     picture:'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fup.enterdesk.com%2Fedpic%2Ffe%2F11%2F97%2Ffe119792285b42e687ebf4367f77c8de.jpg&refer=http%3A%2F%2Fup.enterdesk.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1667467708&t=f250a0a76a390e652a8e7d302b64f987',
        //     name:'莫言',
        //     status:'正常',
        //     createtime:'2022-07-21 08:25:00',
        //     des:"当代作家，获诺贝尔文学奖，代表作《红高粱》"
        // },
        // {
        //     id:4,
        //     App:'小黑早教',
        //     picture:'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fup.enterdesk.com%2Fedpic%2Ffe%2F11%2F97%2Ffe119792285b42e687ebf4367f77c8de.jpg&refer=http%3A%2F%2Fup.enterdesk.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1667467708&t=f250a0a76a390e652a8e7d302b64f987',
        //     name:'张三',
        //     status:'冻结',
        //     createtime:'2022-08-01 12:45:37',
        //     des:"当代作家"
        // },
        // {
        //     id:5,
        //     App:'小黑早教',
        //     picture:'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fup.enterdesk.com%2Fedpic%2Ffe%2F11%2F97%2Ffe119792285b42e687ebf4367f77c8de.jpg&refer=http%3A%2F%2Fup.enterdesk.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1667467708&t=f250a0a76a390e652a8e7d302b64f987',
        //     name:'李雪琴',
        //     status:'正常',
        //     createtime:'2022-09-27 19:29:48',
        //     des:"幽默风趣的才女"
        // },
        // {
        //     id:6,
        //     App:'小黑早教',
        //     picture:'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fup.enterdesk.com%2Fedpic%2Ffe%2F11%2F97%2Ffe119792285b42e687ebf4367f77c8de.jpg&refer=http%3A%2F%2Fup.enterdesk.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1667467708&t=f250a0a76a390e652a8e7d302b64f987',
        //     name:'饶雪漫',
        //     status:'冻结',
        //     createtime:'2022-10-04 09:47:56',
        //     des:"女作家，编剧，作词人"
        // },

    ],

    //获取所有作者信息数据
    async getAllauthor(val){
        await showAllauthor(val).then(res=>{
            console.log(res);
            if (res.code===200){
                this.authordata=res.data
            }else {
                return message.error('请求异常！')
            }
        }).catch(err=>{
            throw err
            }
        )
    },
    //多条件查询作者信息
    async getQueryauthor(val){
        await showWriterByStateOrWriterName(val).then(res=>{
            console.log(res);
            if (res.code===200){
                this.authordata=res.data
            }else {
                return message.error('请求异常！')
            }
        }).catch(err=>{
            throw err
            }
        )
    },
    
    //添加作者
    async addAuthor(val){
        await addWriterInfo(val).then(res=>{
            if (res.code===200){
                this.getAllauthor(1);
            }else {
                return message.error('请求异常！')
            }
        }).catch(err=>{
            throw err
            }
        )
    },

    // 批量修改作者状态
    async editAuthorState(val,str){
        await updateWriterState(val,str).then(res=>{
            if (res.code===200){
                this.getAllauthor(1);
            }else {
                return message.error('请求异常！')
            }
        }).catch(err=>{
            throw err
            }
        )
    },

    // 编辑作者信息
     editAuthor(val){
         updateWriterInfo(val).then(res=>{
            if (res.code===200){
                this.getAllauthor(1);
            }else {
                return message.error('请求异常！')
            }
        }).catch(err=>{
            throw err
            }
        )
    },
    // 上传作者头像uploadWriterImage
   addAuthorImage(file,val,str){
        console.log(file)
        console.log(val)
        if(str==='编辑'){
            console.log("111111编辑111111")
            if(file==={}){
                // 这里判断的是如果在编辑页面没有去修改图片，那么file就是一个空对象，图片的路径还是之前的路径
                this.editAuthor(val)
            }else{
                uploadWriterImage(file).then(res=>{
                    console.log(res)
                    // 如果有修改图片，则将编辑提交的表单数据对象中的图片地址设置为请求到的图片路径
                    val.headPortrait=res.data
                    this.editAuthor(val)
                }).catch(err=>{
                    console.log(err)
                })  
            } 
        }else{
            console.log("22222222新增2222")
            if(file==={}){
                this.addAuthor(val)
            }else{
                uploadWriterImage(file).then(res=>{
                    console.log(res)
                    // 如何有选择上传图片，则将新增提交的表单数据对象中的图片地址设置为请求到的图片路径
                    val.headPortrait=res.data
                    this.addAuthor(val)
                }).catch(err=>{
                    console.log(err)
                })  
            }
            
        }
        
        
    },
    // 删除作者信息
    async delAuthor(val){
        
        await deleteWriterInfo(val).then(res=>{
            if (res.code===200){
                this.getAllauthor(1);
            }else {
                return message.error('请求异常！')
            }
        }).catch(err=>{
            throw err
            }
        )
    },
    selectAuthors:[],
    setSleAuthor(val){
        this.selectAuthors=val;   
    },
    selectAuthorsID:[],
    setSleAuthorId(val){
        this.selectAuthorsID=val;
    },
    
}